package com.example.baselibrary.widgets;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ImageView;

import com.example.baselibrary.R;

/**
 * 加载对话框封装 by zqq on 2018/6/11.
 */

public class ProgressLoading extends Dialog {

    private static ProgressLoading mDialog;
    private static AnimationDrawable animDrawable;

    private ProgressLoading(@NonNull Context context) {
        super(context);
    }

    private ProgressLoading(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static ProgressLoading create(Context context) {
        mDialog = new ProgressLoading(context, R.style.LightProgressDialog);//样式引入
        //dialog通过此方式进行自定义布局
        mDialog.setContentView(R.layout.progress_dialog);
        mDialog.setCancelable(true);                //是否可以取消
        mDialog.setCanceledOnTouchOutside(false);   //点击外部可以取消
        mDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

        //设置明暗程度
        WindowManager.LayoutParams layoutParams = mDialog.getWindow().getAttributes();
        layoutParams.dimAmount = 0.2f;      //黑暗程度
        //设置属性
        mDialog.getWindow().setAttributes(layoutParams);

        //获取动画视图-------通过此方式获取ImageView
        ImageView loadingView = mDialog.findViewById(R.id.iv_loading);
        // loadingView.background//获取背景色--但是此处是动画所以用以下
        animDrawable = (AnimationDrawable) loadingView.getBackground();

        return mDialog;
    }

    //显示加载对话框，动画开始-
    public void showLoading() {
        super.show();
        animDrawable.start();
    }

    //隐藏加载对话框，动画停止
    public void hideLoading() {
        super.dismiss();
        animDrawable.stop();
    }

}
